package mspbots.qbd.desktop.core;

import lombok.Builder;
import lombok.Data;
import mspbots.qbd.common.ResultSync;
import org.springframework.util.StringUtils;

import java.time.LocalDate;

/**
 * mspbots.data.qbd.ws.SoapSession
 *
 * @author Alex bob(https://github.com/vnobo)
 * @date Created by 2020/6/4
 */
@Data
@Builder
public class SoapSession {

    private Long tenantId;
    private int index;
    private int page;
    private int size;
    private String iteratorId;
    private String action;
    private LocalDate startDate;
    private ResultSync resultSync;

    /**
     * @return default ResultSync body
     */
    public static SoapSession withDefault(Long tenantId, String action) {
        return SoapSession.builder().index(0).size(50).page(0)
                .tenantId(tenantId)
                .startDate(LocalDate.now().minusYears(WebServiceConfig.SYNC_DATA_MINUS_YEARS))
                .resultSync(ResultSync.withDefault(action)
                        .tenantId(tenantId))
                .build();
    }

    public String getIterator() {
        if (StringUtils.isEmpty(this.iteratorId)) {
            return "iterator=\"Start\"";
        } else {
            return "iterator=\"Continue\" iteratorID=\"" + this.iteratorId + "\"";
        }
    }
}
